<?php
/**
 * 教室列表页面
 * 相对路径: usr/list.php
 * 作者: 15058593138@qq.com
 */

// 处理Ajax搜索请求
if (isset($_GET['act']) && $_GET['act'] === 'search') {
    $keyword = isset($_POST['keyword']) ? safeStr($_POST['keyword']) : '';
    
    $rooms = readJson('./inc/weici.json.php');
    $keyue = readJson('./inc/keyue.json.php');
    
    $result = [];
    foreach ($rooms as $room) {
        $roomCode = $room['room'];
        $roomName = $room['name'];
        
        // 搜索过滤
        if (!empty($keyword) && strpos($roomCode . $roomName, $keyword) === false) {
            continue;
        }
        
        // 计算总座位数
        $total = countSeats($room['layout']);
        
        // 计算明天已预定数（示例）
        $tomorrow = date('Ymd', strtotime('+1 day'));
        $db = new DB();
        $db->autoCreateDateTable($tomorrow);
        $recTable = TB_PREFIX . 'recs_' . $tomorrow;
        
        $booked = 0;
        if ($db->tableExists($recTable)) {
            $sql = "SELECT COUNT(*) FROM `{$recTable}` WHERE `room` = '{$roomCode}' AND `stats` = 1";
            $booked = $db->getCount($sql);
        }
        
        $remain = $total - $booked;
        
        $result[] = [
            'room' => $roomCode,
            'name' => $roomName,
            'total' => $total,
            'booked' => $booked,
            'remain' => $remain
        ];
    }
    
    // 按余位排序
    usort($result, function($a, $b) {
        return $b['remain'] - $a['remain'];
    });
    
    jsonMsg(1, '查询成功', $result);
}

require_once './inc/head.php';
?>

<div class="container">
    <div class="search-bar">
        <input type="text" id="searchInput" class="form-control" placeholder="搜索教室编号或名称..." style="flex:3">
        <button class="btn btn-primary" onclick="searchRooms()">搜索</button>
    </div>
    
    <div class="table-wrapper">
        <table class="table">
            <thead>
                <tr>
                    <th>教室编号</th>
                    <th>教室名称</th>
                    <th>总座位</th>
                    <th>已约</th>
                    <th>余位</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="roomList">
                <tr>
                    <td colspan="6" style="text-align:center;">加载中...</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<script>
    let searchTimer;
    
    // 搜索教室
    function searchRooms() {
        const keyword = document.getElementById('searchInput').value;
        
        ajax('index.php?do=list&act=search', {keyword: keyword}, function(res) {
            if (res.code === 1) {
                renderRoomList(res.data);
            } else {
                toast(res.msg, 'error');
            }
        });
    }
    
    // 渲染教室列表
    function renderRoomList(data) {
        const tbody = document.getElementById('roomList');
        
        if (data.length === 0) {
            tbody.innerHTML = '<tr><td colspan="6" style="text-align:center;">暂无数据</td></tr>';
            return;
        }
        
        let html = '';
        data.forEach(room => {
            html += `<tr>
                <td>${room.room}</td>
                <td>${room.name}</td>
                <td>${room.total}</td>
                <td>${room.booked}</td>
                <td style="color:${room.remain > 0 ? '#28a745' : '#dc3545'}">${room.remain}</td>
                <td>
                    <a href="index.php?do=biao&room=${room.room}" class="btn btn-primary btn-sm">查看座位</a>
                </td>
            </tr>`;
        });
        
        tbody.innerHTML = html;
    }
    
    // 输入防抖搜索
    document.getElementById('searchInput').oninput = debounce(function() {
        searchRooms();
    }, 500);
    
    // 页面加载时搜索
    window.onload = function() {
        searchRooms();
    };
</script>

<?php require_once './inc/foot.php'; ?>